from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from home_design.models import StyleComment
from home_design.serializers import StyleCommentCreateSerializer, StyleCommentSerializer


class StyleCommentCreateView(APIView):
    permission_classes = [IsAuthenticatedOrReadOnly]  # 只有登录用户才能创建评论

    def post(self, request):
        """处理POST请求，创建单品设计作品的评论"""
        serializer = StyleCommentCreateSerializer(data=request.data, context={'request': request})
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class StyleCommentDetailView(APIView):

    def get(self, request, style_pk):
        """处理GET请求，获取单品设计作品的评论"""
        style_comments = StyleComment.objects.filter(style_id=style_pk)
        serializer = StyleCommentSerializer(style_comments, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
